package com.example.spotitube.View;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.spotitube.R;


public class SuggestedArtistDialog implements OnItemClickListener{
	
	private ArrayList<String>	artists;
	private Context				context;
	private MainActivity		main;
	private AlertDialog			dialog;
	
	
	public SuggestedArtistDialog( ArrayList<String> artists, Context context, MainActivity main ) {
		this.artists = artists;
		this.context = context;
		this.main = main;
	}
	
	
	public void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder( context );
		
		LayoutInflater factory = LayoutInflater.from( context );
		View view = factory.inflate( R.layout.artist_chooser_dialog, null );
		builder.setView( view );
		ListView suggestionsList = ( ListView ) view.findViewById( R.id.artistSuggestions );
		ArrayAdapter<String> adapter = new ArrayAdapter<String>( context, R.layout.artist_suggestion_list_item, artists ){
			
			@Override
			public View getView( int position, View convertView, ViewGroup parent ) {
				LayoutInflater inflater = ( LayoutInflater ) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
				View listItem = inflater.inflate( R.layout.artist_suggestion_list_item, parent, false );
				
				TextView artistSuggested = ( TextView ) listItem.findViewById( R.id.artistSuggested );
				artistSuggested.setText( artists.get( position ) );
				
				return listItem;
			}
		};
		suggestionsList.setAdapter( adapter );
		suggestionsList.setOnItemClickListener( this );
		dialog = builder.create();
		dialog.show();
	}
	
	
	@Override
	public void onItemClick( AdapterView<?> adapter, View v, int position, long id ) {
		String result = artists.get( position );
		dialog.cancel();
		
		main.setArtistToSearch( result );
	}
	
}
